<?php
/*
 * Created on 21 janv. 2009
 * Created by Ludovic Reenaers
 * lreenaers@hotmail.com
 */
 
 class Controler
{
	private $params;
	private $model;
	private $view;
	private $action;
	private $module;
	protected $translator;
	
	function __construct()
	{
		$this->model = new Model(null,null);
		$this->checkSession();
		$this->translator = new XMLEngine($_SESSION['root_url'].'xml/website.xml', strtolower($_SESSION['langue']));
		$this->params=array();
		$this->getAction();
		$this->createMV();
		
		echo $this->view->getPage();
	}
	private function checkSession(){
		if(!isset($_SESSION["USER_BEAN"]) or strtolower($_SESSION["USER_BEAN"]->getProfile()->getName())=="anonymous"){
			$_SESSION["USER_BEAN"]=$this->model->getAnonymousUser();
			$_SESSION['langue'] = 'Fr' ;
			$_SESSION['langueid'] = 1 ;
		}else{
			$_SESSION['langue'] = $_SESSION["USER_BEAN"]->getLangue()->getAbr() ;
			$_SESSION['langueid'] = $_SESSION["USER_BEAN"]->getLangue()->getId() ;
		}
		if (!isset($_SESSION['root_url'])){
			$_SESSION['root_url']='http://localhost/brahma/';//Could be 'http://localhost/' if root website
		}
		
		if (!isset($_SESSION['controler'])){
			$_SESSION['controler']='/brahma/php/index.php';
		}
		if (!isset($_SESSION['encoding'])){
			$_SESSION['encoding']='UTF-8';
		}
		 
		if(isset($_COOKIE['email'])){
			try{	
				$_SESSION["USER_BEAN"]->setEmail($_COOKIE["email"]);
			}catch (Exception $e){
				
			}
		}
		$_SESSION["langues"]= $this->model->getLangues();
	}
	private function checkSecurity(){
		$boolSec = false;
		if(in_array(strtolower($this->action)."-".strtolower($this->module),$_SESSION["USER_BEAN"]->getProfile()->getTokensStringArray())){
			$boolSec =true;
		}
		return $boolSec;
		
	}
	private function getAction()
	{
		
		
		$url=explode("/",substr($_SERVER["REQUEST_URI"],strlen($_SERVER["SCRIPT_NAME"])+1));
		
		if(count($url)==0 || $url[0]=="")
		{
			$this->action="show";
			$this->module="homepage";
		}
		else
		{
		
			$this->action=$url[0];
			$this->module=strtolower($url[1]);
			$_SESSION["action"]=$this->action;
			$_SESSION["module"]=$this->module;
			if($_SERVER["REQUEST_METHOD"]=="POST")
			{
				$this->params=$_POST;
			}
			else
			{
				for($i=2;$i<count($url)-1;$i++)
				{
					$param=explode("=",$url[$i]);
					#hack utf8??? toujours utile??
					$this->params[htmlspecialchars(urldecode($param[0]))]=htmlspecialchars(urldecode($param[1]));
				}
			}
		}
	}
	function doAction(){
		
		$mpack="models/Model".ucfirst($this->module).".php";
		$vpack="views/View".ucfirst($this->module).".php";
		$m="Model".ucfirst($this->module);
		$v="View".ucfirst($this->module);
		$method=$this->action;
		
		if(file_exists($mpack) && file_exists($vpack))
			{
				
				require($mpack);
				require($vpack);		
				$this->model=new $m($this->params,$this->translator);
				
				$this->view=new $v($this->module,$this->model->getMenuG(),$this->translator);
				if (method_exists($this->view,$method)){
					if($this->checkSecurity()){
						
							$this->view->$method($this->model->$method());
						
					}else{
						$this->view->setContent('<META HTTP-EQUIV="Refresh" CONTENT="0; URL=/brahma/html/error/err_401.html">');
					}
				}else{
						$this->view->setContent('<META HTTP-EQUIV="Refresh" CONTENT="0; URL=/brahma/html/error/err_404.html">');
					}
				
			}
			else{
				$this->model=new Model($this->params);
				$this->view=new View($this->module,$this->model->getMenuG(),"XHTML1.0T");
				$this->view->setContent('<META HTTP-EQUIV="Refresh" CONTENT="0; URL=/brahma/html/error/err_404.html">');
			}
				
	}
	function createMV()
	{
		$this->doAction();
	}	
}
?>
